import HeadPrefab from "../../../component/HeadPrefab";
import ComponentBase from "../../../engine/ComponentBase";
import DateUtil from "../../../engine/DateUtil";
import { EventManager } from "../../../engine/EventManager";
import ViewBase from "../../../engine/ViewBase";
import { ClubSocketClient } from "../../../net/ClubSocketClient";
import { EventConfig } from "../../config/EventConfig";
import { UserModel } from "../../model/UserModel";
import { UIHelper } from "../../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewClubAddGroupMember extends ViewBase {


    private userinfoNode: cc.Node
    private txtInputUserid: cc.EditBox;
    private showData: object
    private groupId: number = 0;


    public onInitChild(id: number) {
        this.groupId = id
    }

    onEnable(): void {
        super.onEnable()
        this.userinfoNode = this.findChild(this.node, "userinfoNode")
        this.userinfoNode.active = false;
        this.txtInputUserid = this.getChild(this.node, "txtInputUserid", cc.EditBox);

        let btnsearch: cc.Node = this.findChild(this.node, "btnsearch")
        UIHelper.onAddEvent(btnsearch, () => {
            if (this.txtInputUserid.string == "") {
                UIHelper.showTip("请输入userid")
                return
            } else {
                this.onClickSearch(parseInt(this.txtInputUserid.string))
            }
        })
    }


    private onClickSearch(userid: number) {
        // let showData: object = null
        ClubSocketClient.req_club_member_one(UserModel.instance.nowClub.clubid, userid, (data: object) => {
            this.showData = data;
            this.refresh()
        })
    }

    private refresh() {
        if (this.showData) {
            this.userinfoNode.active = true
            let head: HeadPrefab = this.getChild(this.userinfoNode, "head", HeadPrefab);
            head.setHead(this.showData["headimg"], this.showData["lv"])
            let txtUserid: cc.Label = this.getChild(this.userinfoNode, "txtUserid", cc.Label);
            txtUserid.string = "" + this.showData["userid"];
            let txtName: cc.Label = this.getChild(this.userinfoNode, "txtName", cc.Label);
            txtName.string = "" + this.showData["nick_name"];
            let txtInvite: cc.Label = this.getChild(this.userinfoNode, "txtInvite", cc.Label);
            if (this.showData["invite"]) {
                txtInvite.string = "" + this.showData["invite"]
            } else {
                txtInvite.string = ""
            }
            let txtGameTimes: cc.Label = this.getChild(this.userinfoNode, "txtGameTimes", cc.Label)
            if (this.showData["gametimes"]) {
                txtGameTimes.string = "" + this.showData["gametimes"]
            } else {
                txtGameTimes.string = "0"
            }
            let txtApply: cc.Label = this.getChild(this.userinfoNode, "txtApply", cc.Label)
            if (this.showData["approver"]) {
                txtApply.string = this.showData["approver"];
            } else {
                txtApply.string = ""
            }
            let txtJoinTime: cc.Label = this.getChild(this.userinfoNode, "txtJoinTime", cc.Label)
            let joinTime: Date = new Date(this.showData["join_time"])
            txtJoinTime.string = DateUtil.formatDateStr(joinTime.getTime())
            let txtLastLogin: cc.Label = this.getChild(this.userinfoNode, "txtLastLogin", cc.Label);
            let last_login_time: Date = new Date(this.showData["last_login_time"])
            txtLastLogin.string = DateUtil.formatDateStr(last_login_time.getTime())
            let btnadd: cc.Node = this.getChild(this.userinfoNode, "btnadd")
            UIHelper.onAddEvent(btnadd, () => {
                ClubSocketClient.req_add_user_to_club_group(this.groupId, this.showData["userid"], () => {
                    this.closeView()
                    EventManager.instance.dispatch(EventConfig.Event_Update_Club_Group)
                })
            })
        } else {
            this.userinfoNode.active = false;
        }
    }

}
